package com.maibow.controller.admin;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.maibow.controller.base.BaseController;
import com.maibow.entity.admin.Module;
import com.maibow.entity.admin.OperPrivilege;
import com.maibow.entity.admin.RoleOperPrivilege;
import com.maibow.service.admin.ModuleService;
import com.maibow.util.PageData;

/**
 * 
 * 脉博网络后台管理系统
 * 包名：com.maibow.controller.admin
 * 文件名：ModuleController.java
 * 作者: 吴比(Wubi)
 * 时间：2015年3月28日-下午5:38:38
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
@Controller
@RequestMapping(value = "/module")
public class ModuleController extends BaseController {

    @Resource(name = "moduleService")
    private ModuleService moduleService;

    /**
     * 
     * list 显示菜单列表
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping
    public ModelAndView list() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        try {
            List<Module> moduleList = moduleService.listAllParentModule();
            modelView.addObject("moduleList", moduleList);
            modelView.setViewName("system/module/module_list");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

        return modelView;
    }

    /**
     * 获取当前菜单的所有子菜单
     * 
     * @param moduleid
     * @param response
     */
    @RequestMapping(value = "/sub")
    public void getSub(@RequestParam String moduleid, HttpServletResponse response) throws Exception {
        try {
            response.setCharacterEncoding("utf-8");
            List<Module> subModule = moduleService.listSubModuleByParentId(Integer.valueOf(moduleid));
            JSONArray arr = JSONArray.fromObject(subModule);
            PrintWriter out = response.getWriter();
            String json = arr.toString();
            out.write(json);
            out.flush();
            out.close();
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }

    /**
     * 
     * toAdd(请求新增菜单页面)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/toAdd")
    public ModelAndView toAdd() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        try {
            List<Module> moduleList = moduleService.listAllParentModule();
            modelView.addObject("moduleList", moduleList);
            modelView.setViewName("system/module/module_add");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return modelView;
    }

    /**
     * 
     * add(保存菜单信息)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param module
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/add")
    public ModelAndView add(Module module) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        // PageData pageData = this.getPageData();
        try {
            // pageData =
            // moduleService.findMaxId(pageData);
            // module.setModuleId(Integer.parseInt(pageData
            // == null ? "0":
            // pageData.get("MID").toString()) +
            // 1);
            module.setSubsystemid(Integer.valueOf(1));
            moduleService.saveModule(module);
            modelView.addObject("msg", "success");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            modelView.addObject("msg", "failed");
        }
        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 请求编辑菜单页面
     * 
     * @param
     * @return
     */
    @RequestMapping(value = "/toEdit")
    public ModelAndView toEdit(String moduleid) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            pageData.put("moduleid", Integer.valueOf(moduleid));
            pageData = moduleService.getModuleById(pageData);
            List<Module> moduleList = moduleService.listAllParentModule();
            modelView.addObject("moduleList", moduleList);
            modelView.addObject("pageData", pageData);
            modelView.setViewName("system/module/module_edit");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return modelView;
    }

    /**
     * 
     * edit(保存编辑)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            String parentmoduleid = pageData.getString("parentmoduleid");
            if (null == parentmoduleid || "".equals(parentmoduleid)) {
                parentmoduleid = "0";
            }

            pageData.put("parentmoduleid", Integer.valueOf(parentmoduleid));
            pageData.put("moduleid", Integer.valueOf(pageData.get("moduleid").toString()));
            pageData = moduleService.edit(pageData);
            modelView.addObject("msg", "success");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            modelView.addObject("msg", "failed");
        }

        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 请求编辑菜单图标页面
     * 
     * @param
     * @return
     */
    @RequestMapping(value = "/toEditIcon")
    public ModelAndView toEditicon(String moduleid) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            pageData.put("moduleid", Integer.valueOf(moduleid));
            modelView.addObject("pageData", pageData);
            modelView.setViewName("system/module/module_icon");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

        return modelView;
    }

    /**
     * 保存菜单图标 (顶部菜单)
     * 
     * @param
     * @return
     */
    @RequestMapping(value = "/editIcon")
    public ModelAndView editicon() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            pageData.put("moduleid", Integer.valueOf(pageData.get("moduleid").toString()));
            pageData = moduleService.editIcon(pageData);
            modelView.addObject("msg", "success");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            modelView.addObject("msg", "failed");
        }
        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 删除菜单
     * delete(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param moduleid
     * 标记：@param out
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/del")
    public void delete(@RequestParam String moduleid, PrintWriter out) throws Exception {
        try {
            List<PageData> roleList = moduleService.queryAllRolesByModuleId(Integer.valueOf(moduleid));
            List<RoleOperPrivilege> roleOperPrivilegeList = moduleService.queryAllOperPrivilegeRelaByModuleId(Integer.valueOf(moduleid));
            if (roleList.size() > 0) {
                out.write("false2");
            } else if (roleOperPrivilegeList.size() > 0) {
                out.write("false3");
            } else {
                moduleService.deleteModuleById(Integer.valueOf(moduleid));
                moduleService.deleteOperPrivilegeByModuleId(Integer.valueOf(moduleid));
                out.write("success");
            }
            out.flush();
            out.close();
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }

    /**
     * 显示操作权限列表
     * 
     * @param model
     * @return
     */
    @RequestMapping(value = "/operPrivilege")
    public ModelAndView operPrivilegeList(@RequestParam String moduleid) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = this.getPageData();
        try {
            pageData.put("moduleid", Integer.valueOf(moduleid));
            List<OperPrivilege> operPrivilegeList = moduleService.listAllOperPrivilege(Integer.valueOf(moduleid));
            modelView.addObject("operPrivilegeList", operPrivilegeList);
            modelView.setViewName("system/module/oper_privilege_list");
            modelView.addObject("pageData", pageData);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

        return modelView;
    }

    /**
     * 
     * toAddOperPrivilege(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param moduleid
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/toAddOperPrivilege")
    public ModelAndView toAddOperPrivilege() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = this.getPageData();
        try {
            modelView.addObject("pageData", pageData);
            modelView.setViewName("system/module/oper_privilege_add");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return modelView;
    }

    /**
     * 
     * addOperPrivilege(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param operPrivilege
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/addOperPrivilege")
    public ModelAndView addOperPrivilege(OperPrivilege operPrivilege) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = this.getPageData();
        try {
            pageData = moduleService.findOperPrivilegeMaxId(pageData);
            operPrivilege.setOperprivilegeid(Integer.parseInt(pageData == null ? "0" : pageData.get("mid").toString()) + 1);
            moduleService.saveOperPrivilege(operPrivilege);
            modelView.addObject("msg", "success");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            modelView.addObject("msg", "failed");
        }
        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 请求编辑菜单页面
     * 
     * @param
     * @return
     */
    @RequestMapping(value = "/toEditOperPrivilege")
    public ModelAndView toEditOperPrivilege(String operprivilegeid) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            pageData.put("operprivilegeid", Integer.valueOf(operprivilegeid));
            pageData = moduleService.getOperPrivilegeById(pageData);
            modelView.addObject("pageData", pageData);
            modelView.setViewName("system/module/oper_privilege_edit");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return modelView;
    }

    /**
     * 
     * editOperPrivilege(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/editOperPrivilege")
    public ModelAndView editOperPrivilege() throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        try {
            pageData = this.getPageData();
            pageData.put("operprivilegeid", Integer.valueOf(pageData.get("operprivilegeid").toString()));
            pageData = moduleService.editOperPrivilege(pageData);
            modelView.addObject("msg", "success");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            modelView.addObject("msg", "failed");
        }

        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 
     * deleteOperPrivilege(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param operprivilegeid
     * 标记：@param out
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/deleteOperPrivilege")
    public void deleteOperPrivilege(@RequestParam String operprivilegeid, PrintWriter out) throws Exception {
        try {
            moduleService.deleteOperPrivilegeById(Integer.valueOf(operprivilegeid));
            out.write("success");
            out.flush();
            out.close();
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }
}
